<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="appStore.sidebar.opened" class="hamburger-container" @toggle-click="toggleSideBar" />
    <breadcrumb id="breadcrumb-container" class="breadcrumb-container" v-if="!settingsStore.topNav" />
    <top-nav id="topMenu-container" class="topMenu-container" v-if="settingsStore.topNav" />

    <div class="right-menu">
      <template v-if="appStore.device !== 'mobile'">
        <el-tooltip content="搜索" effect="dark" placement="bottom">
          <header-search id="header-search" class="right-menu-item" />
        </el-tooltip>
        <el-tooltip content="全屏" effect="dark" placement="bottom">
          <ScreenFull id="screenFull" class="right-menu-item hover-effect" />
        </el-tooltip>
        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>
      </template>
      <div class="avatar-container">
        <el-dropdown @command="handleCommand" class="right-menu-item hover-effect" trigger="click">
          <div class="avatar-wrapper" style="user-select: none;">
            <img :src="userStore.userInfo.avatar" class="user-avatar" alt="" />
            <el-icon>
              <caret-bottom />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>
              <el-dropdown-item command="setLayout">
                <span>布局设置</span>
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import Breadcrumb from '@/layouts/components/Navbar/Breadcrumb.vue'
import TopNav from '@/layouts/components/Navbar/TopNav.vue'
import Hamburger from '@/layouts/components/Navbar/Hamburger.vue'
import ScreenFull from '@/layouts/components/Navbar/FullScreen.vue'
import SizeSelect from '@/layouts/components/Navbar/SizeSelect.vue'
import HeaderSearch from '@/layouts/components/Navbar/HeaderSearch.vue'
import { CaretBottom } from "@element-plus/icons-vue"
import store from '@/store/index.js'


const appStore = store.app
const userStore = store.user
const settingsStore = store.settings

function toggleSideBar() {
  appStore.toggleSideBar()
}

function handleCommand(command) {
  switch (command) {
    case "setLayout":
      setLayout()
      break
    case "logout":
      logout()
      break
    default:
      break
  }
}


function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      location.href = '/'
    })
  }).catch(() => { })
}

const emits = defineEmits(['setLayout'])

function setLayout() {
  emits('setLayout')
}
</script>

<style lang="scss" scoped>
@use "@/assets/styles/variables.module" as layout;

.navbar {
  height: #{layout.$navbar-height};
  overflow: hidden;
  position: relative;
  background: #304156;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 52px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topMenu-container {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 12px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 8px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
          color: #ffffff;
        }
      }
    }
  }
}
</style>
